# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
携程-酒店PC端
携程-酒店评论-手机端
http://m.ctrip.com/html5/hotel/HotelDetail/dianping/5209137.html

"""


class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
        }
        self.headers1 = {
            'authority': 'hotels.ctrip.com',
            'method': 'POST',
            'path': '/Domestic/Tool/AjaxHotelListPrice.aspx',
            'scheme': 'https',
            'accept': '*/*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'max-age=0',
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'cookie': 'magicid=bue4cchJPkLzt3ZLwkEE0xcJvm+Mwqb8g4FbOKZBAUfBaLSQv4yIN4/TI76Mhhde; ASP.NET_SessionId=ez4wo3bob1f0scpjd23mlqkw; _abtest_userid=072854f8-19c2-4674-b828-421bcca0b820; hoteluuid=AS0B29FBFUqPuTp1; OID_ForOnlineHotel=15819832547811xjrhy1581983255002102002; _RF1=42.55.196.218; _RSG=.hKgTvWNNQ2BOKf9vfhJu8; _RDG=2849d7ebdba96c20db1e6f4e295042882c; _RGUID=26975e0e-5da9-4afe-8e13-ae9e49a5460b; MKT_CKID_LMT=1581983259818; MKT_CKID=1581983259818.iqzc8.pvj2; _ga=GA1.2.889846459.1581983260; _gid=GA1.2.219063071.1581983260; MKT_Pagesource=PC; ASP.NET_SessionSvc=MTAuNjEuMjYuNDN8OTA5MHxqaW5xaWFvfGRlZmF1bHR8MTU3NzExNDU1MDI2Mg; IntHotelCityID=splitsplitsplit2020-02-19split2020-02-20splitsplitsplit0splitsplit; __utma=13090024.889846459.1581983260.1581983270.1581983270.1; __utmc=13090024; __utmz=13090024.1581983270.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _HGUID=RVYWU%05P%05MU%04%01YMT%01%06%05MX%05QSM%01%05Y%05TY%01UTVP%02; fcerror=1587386635; _zQdjfing=1336fad5c0863a923a3165bb5fa4cc3a923a275ad0275ad05fa4ccd5c086; HotelDomesticVisitedHotels1=45897298=0,0,4.8,304,/20041900000165prr7D25.jpg,&44763562=0,0,4.9,220,/20081b000001apkhj3753.jpg,; _jzqco=%7C%7C%7C%7C1581983260052%7C1.298012906.1581983259814.1581985407321.1581988583053.1581985407321.1581988583053.undefined.0.0.10.10; __zpspc=9.2.1581988583.1581988583.1%234%7C%7C%7C%7C%7C%23; _bfi=p1%3D102003%26p2%3D102002%26v1%3D18%26v2%3D17; appFloatCnt=9; hoteluuidkeys=PfXY5AYL0e98EZkW5YPYTkYf9ESY6Y4be7nEkbjmTWgYGYaorFAjcNvm8jmY0YTzwm1wH4YaFjaYPYlajSFIdfvfBjfYpYosv4LEbFyDzjSZvsDelaYgTj0Zw0YLYTUv6qIUlYPcw5FxD7edliHMYHY1YQY5YD3vNZeMLYf6i0hY1YXYk6RUYgYpbEoUKs8wHpizaRBQjdr4lYNqJ6gyNrlhYatWBLv5PxHTezqYgsxQhxUnY79iSXwkLjnLEhkJb4Wa0jsrlGJBHis7wa3v3ZROdj8aY6ajNrNqyGFiPbwHqRfaEmUjQqxH8x4HEgfEH9EXBWZ0eB6wcNESnj3keM7i9TYDZrb1eNqe8AxHgipcigsxUdWSHjLTe8FwkNKD8w1TiSTR41jsPezZEatykXvHFi07Etmy1QvGHKt3ESmKF6wAXiSMR9cj3rLSY8QJ9Dyor8TjsMeLNj0TKqXjcSwfDxm8xcGxbmx3UEQ5EloEa8WhtezOwT1EcajHFe48ioaYl0rT4EGfy8LvgMi7aE4GyobvNzKosWkQEohj03etQxTFjOrqsEd9WUheH1jnpYO4jU4xXFx1fx6Ax9SEO8EMZE7TWPAe18wzbEM4js0e9miGfYA7rgZe3oeTfYktEdXw4TW6ziggKgNE61EmoEnXW7medSwcmEoljmXecGi5TY4Ar37eG1eSzEtkYZ0E6HwoBWzOizYcYobY9fi47i5LiX4jXYUYcOJXOwqSEgkwXbjNFYA4RT4yTYXYfDRDaJpojt4RHAvotjgAjhNy6ARUGJh5w4fjblY7YoYDUYo1JMgWSgicdYnovacy8pilPymTyfBJhUYsGvNgwdpEP0RlYNY9HRPSJDQj05RkdvtfjqOi0DYlgJb6Eh5y3Fjn1RMYzYD4j5zwa6vgZ; _bfa=1.1581983254781.1xjrhy.1.1581983254781.1581988579939.2.19; _bfs=1.2; hotelhst=2012709687',
            'dnt': '1',
            'if-modified-since': 'Thu, 01 Jan 1970 00:00:00 GMT',
            'origin': 'https://hotels.ctrip.com',
            'referer': 'https://hotels.ctrip.com/hotel/shanghai2/p1',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'
        }


    # 酒店信息-有价格
    def get_host_list_1(self, page):
        url = f"https://hotels.ctrip.com/hotel/shanghai2/p{page}"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        postData = self.get_params(html)
        url = "https://hotels.ctrip.com/Domestic/Tool/AjaxHotelListPrice.aspx"
        html = HttpUtils.do_request("POST", url, self.headers1, postData)
        data_json = json.loads(html.text)
        for d in data_json:
            pass

    # 酒店信息-无价格
    def get_host_list(self, page):
        url = f"https://hotels.ctrip.com/hotel/shanghai2/p{page}"
        html = HttpUtils.do_request("GET", url, self.headers, "")

        txt_HotelMapStreetJson = re.findall("var HotelMapStreetJson = (.*?);", html.text)[0]
        # 提取页面中的js，并解析(id、名称)
        txt_hotelPositionJSON = re.findall("hotelPositionJSON:  (.*?)],", html.text)[0] + "]"
        data_json = json.loads(txt_hotelPositionJSON)
        for d in data_json:
            dict_data = dict()
            id = d['id']  # id
            dict_data['name'] = d['name']  # 名称
            dict_data['lat'] = d['lat']  # lat
            dict_data['lon'] = d['lon']  # lon
            dict_data['address'] = d['address']  # 地址
            dict_data['score'] = d['score']  # 总评分
            dict_data['dpscore'] = d['dpscore']  # 用户推荐百分比
            dict_data['dpcount'] = d['dpcount']  # 点评数量
            dict_data['star'] = d['star']  # 酒店星级
            dict_data['stardesc'] = d['stardesc']  # 酒店类型
            print(str(dict_data))

    # 提取页面中的参数，构造Form data
    def get_params(self, html):
        root = etree.HTML(html.text)
        VIEWSTATEGENERATOR = root.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')[0]
        cityName = root.xpath('//input[@id="cityName"]/@value')[0]
        cityId = root.xpath('//input[@id="cityId"]/@value')[0]
        cityPY = root.xpath('//input[@id="cityPY"]/@value')[0]
        cityCode = root.xpath('//input[@id="cityCode"]/@value')[0]
        hidTestLat = root.xpath('//input[@id="hidTestLat"]/@value')[0]
        AllHotelIds = root.xpath('//input[@id="AllHotelIds"]/@value')[0]
        traceAdContextId = root.xpath('//input[@id="traceAdContextId"]/@value')[0]
        pyramidHotels = root.xpath('//input[@id="pyramidHotels"]/@value')[0]
        StartTime = root.xpath('//input[@id="txtCheckIn"]/@value')[0]
        DepTime = root.xpath('//input[@id="txtCheckOut"]/@value')[0]
        checkIn = root.xpath('//input[@id="checkIn"]/@value')[0]
        checkOut = root.xpath('//input[@id="checkOut"]/@value')[0]
        part1_dict = {}
        part2_dict = {}
        part3_dict = {}
        part1_list = []
        part2_list = []
        part3_list = []
        part1_selector = root.xpath('//li[@class="hotel_price_icon"]')
        part2_selector = root.xpath('//div[@class="hotel_new_list J_HotelListBaseCell"]')
        part3_selector = root.xpath('//p[@class="medal_list"]')
        for p1 in part1_selector:
            part1_dict['id'] = p1.xpath('./div/@hotel')[0]
            part1_dict['val'] = p1.xpath('./div/@data-data')[0]
            part1_list.append(part1_dict)
        for p2 in part2_selector:
            part2_dict['id'] = p2.xpath('./@id')[0]
            part2_dict['val'] = p2.xpath('./@data-maidian')[0]
            part2_list.append(part2_dict)
        for p3 in part3_selector:
            part3_dict['id'] = p3.xpath('./span/@data-id')[0]
            part3_dict['val'] = p3.xpath('./span/@data')[0]
            part3_list.append(part3_dict)
        # 提取参数
        hotelIds = re.findall("hotelIds : \'(.*?)\',", html.text, re.DOTALL)[0]
        pList = {
            'part1':part1_list,
            'part2':part2_list,
            'part3':part3_list
        }
        postData = {
            '__VIEWSTATEGENERATOR':VIEWSTATEGENERATOR,
            'cityName':cityName,
            'StartTime': StartTime,
            'DepTime': DepTime,
            'RoomGuestCount': '1,1,0',
            'txtkeyword':'',
            'Resource':'',
            'Room':'',
            'Paymentterm':'',
            'BRev':'',
            'Minstate':'',
            'PromoteType':'',
            'PromoteDate':'',
            'operationtype': 'NEWHOTELORDER',
            'PromoteStartDate':'',
            'PromoteEndDate':'',
            'OrderID':'',
            'RoomNum':'',
            'IsOnlyAirHotel': 'F',
            'cityId':cityId,
            'cityPY':cityPY,
            'cityCode':cityCode,
            'cityLat': '31.2363508011',
            'cityLng': '121.4802384079',
            'positionArea':'',
            'positionId':'',
            'hotelposition':'',
            'keyword':'',
            'hotelId':'',
            'htlPageView': '0',
            'hotelType': 'F',
            'hasPKGHotel': 'F',
            'requestTravelMoney': 'F',
            'isusergiftcard': 'F',
            'useFG': 'F',
            'HotelEquipment':'',
            'priceRange': '-2',
            'hotelBrandId':'',
            'promotion': 'F',
            'prepay': 'F',
            'IsCanReserve': 'F',
            'OrderBy': '99',
            'OrderType':'',
            'k1':'',
            'k2':'',
            'CorpPayType':'',
            'viewType':'',
            'checkIn': checkIn,
            'checkOut': checkOut,
            'DealSale':'',
            'ulogin':'',
            'hidTestLat':hidTestLat,
            'AllHotelIds':AllHotelIds,
            'psid':'',
            'isfromlist': 'T',
            'ubt_price_key': 'htl_search_result_promotion',
            'showwindow':'',
            'defaultcoupon':'',
            'isHuaZhu': 'False',
            'hotelPriceLow':'',
            'unBookHotelTraceCode':'',
            'showTipFlg':'',
            'traceAdContextId':traceAdContextId,
            'allianceid': '0',
            'sid': '0',
            'pyramidHotels':pyramidHotels,
            'hotelIds':hotelIds,
            'markType': '0',
            'page': '1',
            'zone':'',
            'location':'',
            'type':'',
            'brand':'',
            'group':'',
            'feature':'',
            'equip':'',
            'bed':'',
            'breakfast':'',
            'other':'',
            'star':'',
            'sl':'',
            's':'',
            'l':'',
            'price':'',
            'a': '0',
            'keywordLat':'',
            'keywordLon':'',
            'contrast': '0',
            'PaymentType':'',
            'CtripService':'',
            'promotionf':'',
            'allpoint':'',
            'pList':pList,
            'contyped': '0',
            'productcode':'',
            'eleven':'354c84d8bbcb64c1430e43ba8a315243639ca45e084fd4517773a735e3f9f61c_329799566',
        }
        return postData



    # 酒店信息
    def get_contents(self, cityid, star, page):
        headers = {
            'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Origin': 'https://hotels.ctrip.com',
            'Referer': 'https://hotels.ctrip.com/hotel/beijing1',
            'accept': '*/*',
            "cookie": '_abtest_userid=9efbee40-54c0-4fd1-b3fd-d40e8ca0e924; ASP.NET_SessionId=cej0iamovj3xggghos5guz3u; _RF1=60.17.202.154; _RGUID=f61ceaa6-d83a-4ae5-86a3-f2cd7c5f81a0; _RDG=28995c5f732cff20c016df127a65b84f87; _RSG=bhaEORgwSOFW6uNyriyZQ8; Session=SmartLinkCode=U155952&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=&SmartLinkLanguage=zh; MKT_CKID_LMT=1578058175629; MKT_CKID=1578058175628.71ek7.nysg; _ga=GA1.2.900408769.1578058176; _gid=GA1.2.1172423627.1578058176; MKT_Pagesource=PC; GUID=09031029310945081253; Union=OUID=&AllianceID=66672&SID=1693366&SourceID=&AppID=&OpenID=&createtime=1578060087&Expires=1578664887410; ASP.NET_SessionSvc=MTAuNjEuMjYuNDJ8OTA5MHxqaW5xaWFvfGRlZmF1bHR8MTU3NzExNDU1MDUwOA; appFloatCnt=10; _gat=1; _jzqco=%7C%7C%7C%7C1578058175901%7C1.392804500.1578058175626.1578062749409.1578063718057.1578062749409.1578063718057.undefined.0.0.11.11; __zpspc=9.2.1578062171.1578063718.4%234%7C%7C%7C%7C%7C%23; _bfa=1.1578058169664.4efgrb.1.1578061824956.1578062218234.3.29.228032; _bfs=1.8; _bfi=p1%3D102002%26p2%3D10650032916%26v1%3D29%26v2%3D28',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
        }
        postData = {
            'cityId': cityid,
            # 'star': star,
            'page': page,
        }
        url = "https://hotels.ctrip.com/Domestic/Tool/AjaxHotelList.aspx"
        html = HttpUtils.do_request("POST", url, headers, postData)
        data_json = json.loads(html.text)
        hotelList = data_json['hotelList']  # 价格信息
        for d in data_json['hotelPositionJSON']:
            dict_data = dict()
            id = d['id']  # id
            dict_data['name'] = d['name']  # 名称
            dict_data['lat'] = d['lat']  # lat
            dict_data['lon'] = d['lon']  # lon
            dict_data['address'] = d['address']  # 地址
            dict_data['score'] = d['score']  # 总评分
            dict_data['dpscore'] = d['dpscore']  # 用户推荐百分比
            dict_data['dpcount'] = d['dpcount']  # 点评数量
            dict_data['star'] = d['star']  # 酒店星级
            dict_data['stardesc'] = d['stardesc']  # 酒店类型
            print(str(dict_data))

    # 评论内容
    def get_comments(self, hotelid, pagenum):
        postData = {
            "hotelId":hotelid,# 5209137,
            "pageIndex":pagenum,#1,
            "tagId":0,
            "pageSize":10,
            "groupTypeBitMap":2,
            "needStatisticInfo":0,
            "order":0,
            "basicRoomName":"",
            "travelType":-1,
            "head":{
                "cid":"09031029310945081253",
                "ctok":"",
                "cver":"1.0",
                "lang":"01",
                "sid":"8888",
                "syscode":"09","auth":"","extension":[]}}
        postData = json.dumps(postData)
        url = "http://m.ctrip.com/restapi/soa2/16765/gethotelcomment?&_fxpcqlniredt=09031035310779349531"
        time.sleep(1)
        html = HttpUtils.do_request("POST", url, self.headers, postData)
        data_json = json.loads(html.text)
        for d in data_json['othersCommentList']:
            dict_data = dict()
            dict_data['userNickName'] = d['userNickName']  # 用户名称
            dict_data['checkInDate'] = d['checkInDate']  # 入住时间
            dict_data['postDate'] = d['postDate']  # 评论时间
            dict_data['travelType'] = d['travelType']  # 旅行类型
            dict_data['baseRoomName'] = d['baseRoomName']  # 房型
            dict_data['ratingPoint'] = d['ratingPoint']  # 评分
            dict_data['ratingPointDesc'] = d['ratingPointDesc']  # 评分摘要
            dict_data['usefulNumber'] = d['usefulNumber']  # 有用数量
            dict_data['content'] = d['content']  # 评论内容
            print(str(dict_data))
        self.get_comments(hotelid, pagenum + 1)


if __name__ == '__main__':
    test_get = Test_Get()
    # test_get.get_host_list_1(1)
    # test_get.get_contents("2", "", 1)
    test_get.get_comments("661081", 1)